package org.standard.message.action;

import java.io.File;
import java.sql.SQLException;
import java.util.Date;
import java.util.List;

import org.apache.commons.fileupload.FileUploadException;
import org.standard.base.model.BaseUser;
import org.standard.commons.model.Annex;
import org.standard.commons.struts2.BaseAction;
import org.standard.commons.utils.Constants;
import org.standard.commons.utils.FlipPageInfo;
import org.standard.message.model.Message;

public class MessageAction extends BaseAction {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private Message message;
	private FlipPageInfo flipPageInfo = new FlipPageInfo();
	
	private File[] annex;
	private String[] annexContentType;
	private String[] annexFileName;
	private List<Annex> annexList;
	
	private String s_theme;
	private String s_content;
	
	private final static String SEND_SUCCESS = "0";
	private final static String SEND_DELETE = "1";
	

	public String index() {
		
		return CREATE;
	}
	
	public String create() {
		
		BaseUser user = (BaseUser) session.get(Constants.SESSION_USER);
		message.setSendUserId(user.getUserId());
		message.setSendUserName(user.getUserName());
		message.setSendStatus(SEND_SUCCESS);
		message.setDate(new Date());
		try {
			service.createPOJOAndAnnex(message, request, annex, annexFileName, annexContentType);
		} catch (FileUploadException e) {
			setERROR(e.getMessage());
			e.printStackTrace();
			return CREATE;
		}
		return receive();
	}

	public String receive() {
		BaseUser user = (BaseUser) session.get(Constants.SESSION_USER);
		String [][]search = {
				{"theme", s_theme},
				{"content", s_content},
				{"receiveUserId", user.getUserId()}
		};
		String [][]sort = {
				{"date", "1"},
		};
		flipPageInfo = service.findPOJOList(Message.class, flipPageInfo , search, sort);
		return "receive";
	}
	public String send() {
		BaseUser user = (BaseUser) session.get(Constants.SESSION_USER);
		String [][]search = {
				{"theme", s_theme},
				{"content", s_content},
				{"sendUserId", user.getUserId()},
				{"sendStatus", SEND_SUCCESS}
		};
		String [][]sort = {
				{"date", "1"},
		};
		flipPageInfo = service.findPOJOList(Message.class, flipPageInfo , search, sort);
		return "send";
	}
	@SuppressWarnings("unchecked")
	public String modifyPre() {
		
		message = (Message) service.findPOJOById(Message.class, message.getId());
		annexList = service.findPOJOList("from Annex where tableName = '"+Message.class.getSimpleName()+"' and tableId = '"+message.getId()+"'");
		return MODIFY;
	}
	public String detail() {
		
		modifyPre();
		return DETAIL;
	}
	public String modify() {
		
		message.setDate(new Date());
		try {
			service.modifyPOJOAndAnnex(message, request, annex, annexFileName, annexContentType, message.getId());
		} catch (FileUploadException e) {
			setERROR(e.getMessage());
			e.printStackTrace();
			return MODIFY;
		} catch (SQLException e) {
			setERROR(e.getMessage());
			e.printStackTrace();
			return MODIFY;
		}
		return receive();
	}
	public String deleteSend() {
		message = (Message) service.findPOJOById(Message.class, message.getId());
		message.setSendStatus(SEND_DELETE);
		service.modifyPOJO(message);
		return send();
	}
	public String deleteReceive() {
		BaseUser user = (BaseUser) session.get(Constants.SESSION_USER);
		message = (Message) service.findPOJOById(Message.class, message.getId());
		message.setReceiveUserId(message.getReceiveUserId().replace(user.getUserId()+",", ""));
		service.modifyPOJO(message);
		return receive();
	}

	public Message getMessage() {
		return message;
	}

	public void setMessage(Message message) {
		this.message = message;
	}

	public FlipPageInfo getFlipPageInfo() {
		return flipPageInfo;
	}

	public void setFlipPageInfo(FlipPageInfo flipPageInfo) {
		this.flipPageInfo = flipPageInfo;
	}

	public File[] getAnnex() {
		return annex;
	}

	public void setAnnex(File[] annex) {
		this.annex = annex;
	}

	public String[] getAnnexContentType() {
		return annexContentType;
	}

	public void setAnnexContentType(String[] annexContentType) {
		this.annexContentType = annexContentType;
	}

	public String[] getAnnexFileName() {
		return annexFileName;
	}

	public void setAnnexFileName(String[] annexFileName) {
		this.annexFileName = annexFileName;
	}

	public List<Annex> getAnnexList() {
		return annexList;
	}

	public void setAnnexList(List<Annex> annexList) {
		this.annexList = annexList;
	}

	public String getS_theme() {
		return s_theme;
	}

	public void setS_theme(String s_theme) {
		this.s_theme = s_theme;
	}

	public String getS_content() {
		return s_content;
	}

	public void setS_content(String s_content) {
		this.s_content = s_content;
	}
	

}
